package com.fh.web.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import com.fh.bo.PurviewBo;
import com.fh.dao.PurPurviewMapper;
import com.fh.dao.SysUserMapper;
import com.fh.dto.PurPurview;
import com.fh.dto.PurPurview2Role;
import com.fh.dto.SysUser;
import com.fh.web.constants.UserConstants;
import com.fh.web.service.CustomService;
import com.fh.web.util.HttpUtil;
import com.fh.web.util.PropertiesReader;
import com.fh.web.vo.ResultJsonVo;
import com.google.gson.Gson;

import tk.mybatis.mapper.entity.Example;
import tk.mybatis.mapper.entity.Example.Criteria;

/**
 * 用户管理service接口 实现类
 * 
 * @author admin
 */
@Service("customService")
public class CustomServiceImpl implements CustomService {
	private final Logger LOGGER = LoggerFactory.getLogger(getClass());

	@Resource
	private PurPurviewMapper purPurviewMapper;
	@Resource
	private SysUserMapper sysUserDao;

	@Override
	public List<PurPurview> getUserPurview(SysUser sysUser) {
		if (sysUser.getUserCode().equals(UserConstants.SUPERADMIN)) {
			// 系统默认超级管理员，默认拥有所有权限点
			Example example = new Example(PurPurview.class);
			Criteria criteria = example.createCriteria();
			criteria.andNotEqualTo("purType", 1);
			List<PurPurview> purPurviewList = purPurviewMapper.selectByExample(example);
			return purPurviewList;
		} else {
			// 其他用户要根据角色来确定用户拥有的权限
			List<PurPurview2Role> list = sysUserDao.getUserPurIds(sysUser);
			List<Long> list1 = new ArrayList<>();
			if ((list != null) && (list.size() > 0)) {
				for (PurPurview2Role rr : list) {
					list1.add(Long.valueOf(rr.getPurId()));
				}
				HashMap<String, Object> param = new HashMap<String, Object>();
				param.put("item", list1);
				param.put("purTypeC", UserConstants.PURTYPEC);
				param.put("purTypeM", UserConstants.PURTYPEM);
				return sysUserDao.getUserPurview(param);
			}else{
				return null;
			}
		}
	}

	@Override
	public Map<String, Object> userCanOperation(SysUser user, String pId) throws Exception {
		Map<String, Object> resMap = new HashMap<String, Object>();
		List<PurPurview> userPurList = null;
		if (user.getUserCode().equals(UserConstants.SUPERADMIN)) {
			// 系统默认超级管理员，默认拥有权限点下的所有操作权限点
			userPurList = purPurviewMapper.getOperPurByPurId(pId);
		} else {
			// 其他用户要根据角色来确定用户拥有的权限
			userPurList = purViewOperation(user.getUserId().toString(), pId);
		}
		for (int i = 0; i < userPurList.size(); i++) {
			resMap.put(userPurList.get(i).getPurCode(), true);
		}
		return resMap;
	}

	public List<PurPurview> purViewOperation(String uId, String pId) throws Exception {
		return sysUserDao.getPurViewOperation(uId, pId);
	}

	@Override
	public boolean checkPassword(Long userId, String oldPassword) {
		SysUser sysUser = sysUserDao.selectByPrimaryKey(userId);
		if(null==sysUser){
			return false;
		}
		if (oldPassword.equals(sysUser.getPassword())) {
			return true;
		}
		return false;
	}

	@Override
	public boolean updatePassword(Long userId, String password) {
		SysUser sysUser = new SysUser();
		sysUser.setUserId(userId);
		sysUser.setPassword(password);
		sysUser.setUpdateTime(new Date());
		//访问用户中心的url修改用户登录密码
		if(updatefhukPassword(sysUser)){
			//修改用户门户的密码
			sysUserDao.updateByPrimaryKeySelective(sysUser);
			return true;
		}
		return false;
	}
	private boolean updatefhukPassword(SysUser sysUser) {
		//将对象json格式化
		String param="userId="+sysUser.getUserId()+"&password="+sysUser.getPassword();
		final String url = PropertiesReader.getInstance("/fh-web.properties").getValue("updatePassUrl");
		String result=HttpUtil.sendPost(url, param);
		Gson gson = new Gson();
		ResultJsonVo resu = gson.fromJson(result, ResultJsonVo.class);
		if(resu.isSuccess()){
			return true;
		}
		return false;
	}
}
